package com.ruoyi.zhmk.utils;

import java.io.*;

/**
 * @Author: wwy
 * @Date: 2021/6/24 15:12
 */
public class ImageIOUtils {
    public static byte[] read(String name) throws IOException {
        File file = new File(name);

        if (!file.exists() || !file.isFile()){
            return null;
        }


        FileInputStream fis = new FileInputStream(name);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024 * 1];
        for (int length = fis.read(buffer); length > 0; length = fis.read(buffer)){
            baos.write(buffer, 0, length);
        }

        baos.flush();
        byte[] bytes = baos.toByteArray();
        close(baos);
        close(fis);
        return bytes;
    }

    public static void write(byte[] data, String name) throws IOException {
        FileOutputStream fos = new FileOutputStream(name);
        fos.write(data);
        fos.flush();
        close(fos);
    }

    public static void close(Closeable closeable) {
        if (closeable == null){
            return;
        }


        try {
            closeable.close();
        } catch (IOException e) {

        }
    }
}
